package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.entities.BillDetailsEntity;
import com.xilihui.xlh.business.entities.StoreBillDetailsEntity;
import com.xilihui.xlh.business.requests.MineRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/7/10.
 */

public class BillingDetailsActivity extends BaseCompatActivity{
    
    @BindView(R.id.tv_title)
    TextView tv_type;
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_time_type)
    TextView tv_time_type;
    @BindView(R.id.tv_dzTime)
    TextView tv_dzTime;
    @BindView(R.id.tv_isYuYue)
    TextView tv_isYuyue;
    @BindView(R.id.tv_zzType)
    TextView tv_zfType;
    @BindView(R.id.tv_fukuangJiner)
    TextView tv_fukuangJiner;
    @BindView(R.id.tv_fwyj)
    TextView tv_fwyj;
    @BindView(R.id.tv_fk_danhao)
    TextView tv_fkDanhao;
    
    @BindView(R.id.tv_hint_status)
    TextView tv_hint_status;
    @BindView(R.id.tv_hint_isYuYue)
    TextView tv_hint_isYuYue;
    @BindView(R.id.tv_hint_fukuangJiner)
    TextView tv_hint_fukuangjiner;
    @BindView(R.id.tv_hint_zzType)
    TextView tv_hint_zzType;
    @BindView(R.id.ll_khfuyj)
    LinearLayout ll_khfuyj;
    @BindView(R.id.ll_fukdh)
    LinearLayout ll_fukdh;
    
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerView2;
    @BindView(R.id.tv_link)
    TextView tv_link;
    
    String id = "";
    String phone = "";
    String type = "";
    
    ArrayList<BillDetailsEntity.DataBean.ServerAccountBean.WorkerAppointBean> server = new ArrayList<>();
    ArrayList<BillDetailsEntity.DataBean.DeductBean> deduct = new ArrayList<>();
    
    BaseAdapter<BillDetailsEntity.DataBean.ServerAccountBean.WorkerAppointBean> baseAdapter;
    BaseAdapter<BillDetailsEntity.DataBean.DeductBean> baseAdapter2;
    
    @Override
    public int getLayout() {
        return R.layout.activity_bill_details;
    }

    @Override
    public String initTitle() {
        return "账单详情";
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        
        if(type.equals("0")){
            
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView2.setLayoutManager(new LinearLayoutManager(this));
            baseAdapter = new BaseAdapter<BillDetailsEntity.DataBean.ServerAccountBean.WorkerAppointBean>(this,server) {
                @Override
                protected int setLayoutId() {
                    return R.layout.recycyler_item_bill_details;
                }

                @Override
                protected void getView(BaseViewHolder holder, BillDetailsEntity.DataBean.ServerAccountBean.WorkerAppointBean entity, int position) {
                    holder.setText(R.id.tv_zwsc,entity.getTime_diff());
                    holder.setText(R.id.tv_zwsyfy,entity.getSeat_money());
                    holder.setText(R.id.tv_yymd,entity.getAgent().getName());
                    holder.setText(R.id.tv_type,entity.getHair_project().getName());
                    holder.setText(R.id.tv_danhao,entity.getAppoint_num());
                }
            };

            baseAdapter2 = new BaseAdapter<BillDetailsEntity.DataBean.DeductBean>(this,deduct) {
                @Override
                protected int setLayoutId() {
                    return R.layout.recycler_item_bill_kf;
                }
                
                @Override
                protected void getView(BaseViewHolder holder, BillDetailsEntity.DataBean.DeductBean entity, int position) {
                    holder.setText(R.id.tv_kfje,entity.getMoney());
                    holder.setText(R.id.tv_kf_type,entity.getDeduct_type());
                    holder.setText(R.id.tv_kf_danhao,entity.getOrder_num());
                    holder.setText(R.id.tv_kf_time,entity.getAdd_time());
                    holder.setText(R.id.tv_kf_explain,entity.getRemark());
                }
            };

            recyclerView.setAdapter(baseAdapter);
            recyclerView2.setAdapter(baseAdapter2);
        }
        
        tv_link.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + phone);
                intent.setData(data);
                startActivity(intent);
            }
        });
        
                    
    }

    @Override
    public void initData() {
        getData();
    }
    
    public void getData(){
        
        if(type.equals("0")){
            MineRequest.billDetail(BillingDetailsActivity.this,id)
                    .compose(DoTransform.<BillDetailsEntity>applyScheduler(this,true))
                    .compose(this.<BillDetailsEntity>bindToLifecycle())
                    .subscribe(new HttpSubscriber<BillDetailsEntity>(this) {
                        @Override
                        protected void onSuccess(BillDetailsEntity entity) {
                            tv_type.setText(entity.getData().getZaitu_daozhang());
                            tv_num.setText("￥"+entity.getData().getMoney());
                            tv_status.setText(entity.getData().getDaozhang());

                            if(entity.getData().getStatus().equals("0")){
                                tv_time_type.setText("预计到账时间");
                                tv_dzTime.setText(entity.getData().getYuji_time());
                            }else {
                                tv_time_type.setText("到账时间");
                                tv_dzTime.setText(entity.getData().getDaozhang_time());
                            }

                            tv_isYuyue.setText(entity.getData().getYuyue());
                            tv_zfType.setText(entity.getData().getPay_type());
                            tv_fukuangJiner.setText("￥"+entity.getData().getServer_account().getMoney());
                            tv_fwyj.setText(entity.getData().getFuwuyongjin());
                            tv_fkDanhao.setText(entity.getData().getOrder_num());

                            server = (ArrayList<BillDetailsEntity.DataBean.ServerAccountBean.WorkerAppointBean>) entity.getData().getServer_account().getWorker_appoint();
                            deduct = (ArrayList<BillDetailsEntity.DataBean.DeductBean>) entity.getData().getDeduct();

                            baseAdapter.setList(server);
                            baseAdapter2.setList(deduct);

                            phone = entity.getData().getPhone();
                            
                        }

                        @Override
                        protected void onFail(String msg) {
                            ToastUtil.toastShortNegative(msg);
                        }

                    });
        }else {//商品账单详情
            
            MineRequest.storeBillDetail(BillingDetailsActivity.this,id)
                    .compose(DoTransform.<StoreBillDetailsEntity>applyScheduler(this,true))
                    .compose(this.<StoreBillDetailsEntity>bindToLifecycle())
                    .subscribe(new HttpSubscriber<StoreBillDetailsEntity>(this) {
                        @Override
                        protected void onSuccess(StoreBillDetailsEntity entity) {
                            
                            String type = entity.getData().getStatusX();
                            if(type.equals("0")){
                                //在途
                                tv_type.setText("在途分销佣金");
                                tv_status.setText("未到账");
                                tv_time_type.setText("预计到账时间");
                                tv_dzTime.setText(entity.getData().getYuji_time());
                            }else {//到账
                                tv_type.setText("到账分销佣金");
                                tv_status.setText("已到账");
                                tv_dzTime.setText(entity.getData().getExtract_time());
                            }
                            
                            tv_num.setText("￥"+entity.getData().getDeduct_money());
                            tv_hint_isYuYue.setText("商品总价");
                            tv_isYuyue.setText("￥"+entity.getData().getOrder().getOrder_amount());
                            tv_hint_fukuangjiner.setText("付款时间");
                            tv_fukuangJiner.setText(entity.getData().getOrder().getPay_time());
                            
                            tv_hint_zzType.setText("可获分销佣金");
                            tv_zfType.setText("￥"+entity.getData().getDeduct_money());
                            ll_khfuyj.setVisibility(View.GONE);
                            ll_fukdh.setVisibility(View.GONE);
                            phone = entity.getData().getPhone();
                            
                        }
                                        
                        @Override
                        protected void onFail(String msg) {
                            ToastUtil.toastShortNegative(msg);
                        }
                    });
            
        }
                
    }
    
}
